package handlers

import (
	"github.com/gin-gonic/gin"
	"repo-manage/model"
	"strconv"
)

func Add(c *gin.Context) {
	good_name := c.PostForm("good")
	good_price := c.PostForm("good_price")
	good_no := c.PostForm("good_no")
	if good_name == "" || good_price == "" || good_no == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不为空",
		})
		return
	}
	data := model.Good{
		Good:      good_name,
		GoodPrice: good_price,
		GoodNo:    good_no,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "成功",
		})
		return
	}

}
func GoodList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var goods []model.Good
	model.DB.Offset(offset).Limit(size).Find(&goods)
	var total int64
	model.DB.Model(&model.Good{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  goods,
		"total": total,
	})

}
